package org.misalen.web.admin.controllers;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.pro.ProWebAdvertisingCarrier;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.service.file.InfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

@Controller
@RequestMapping(value = "/pro-web-advertising-carrierold")
@Deprecated
public class ProWebAdvertisingCarrierControllerOld extends BaseController {
	
	Configuration cfg = new Configuration(new Version(2, 3, 0));
	{
		cfg.setClassForTemplateLoading(ProWebAdvertisingCarrierControllerOld.class, "/");
		cfg.setDefaultEncoding("utf-8");
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertisingCarrier/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(ProWebAdvertisingCarrier proWebAdvertisingCarrier,
			PaginationFrom<ProWebAdvertisingCarrier> pagination) {
		PaginationFrom<ProWebAdvertisingCarrier> from = proWebAdvertisingCarrierRepository
				.findPageByBean(proWebAdvertisingCarrier, pagination);
		return renderSuccess(from);
	}

	/**
	 * 添加页面
	 * 
	 * @return 添加目标页
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	public ModelAndView add() {
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertisingCarrier/add");
		return modelAndView;
	}

	/**
	 * 添加页面
	 * 
	 * @return 添加目标页
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public ModelAndView addPost(ProWebAdvertisingCarrier proWebAdvertisingCarrier, String content)
			throws UnsupportedEncodingException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("title", proWebAdvertisingCarrier.getName());
		map.put("content", content);
		String string = bulidHtml(map, "template/advertising-webpage.ftl");
		String name = getUUID() + ".html";
		String url = ossFile.uploadFile(new ByteArrayInputStream(string.getBytes("utf-8")), name, InfoType.htmlCode);
		proWebAdvertisingCarrier.setAddTime(new Date());
		proWebAdvertisingCarrier.setUrl(url);
		proWebAdvertisingCarrierRepository.save(proWebAdvertisingCarrier);
		return new ModelAndView("/result/success");
	}

	private String bulidHtml(Map<String, String> data, String templatePath) {
		try {
			StringWriter out = new StringWriter();
			Template template = cfg.getTemplate(templatePath);
			template.process(data, out);
			String string = out.toString();
			out.close();
			return string;
		} catch (Exception e) {
			return null;
		}

	}
	// /**
	// * 添加动作
	// *
	// * @param 信息
	// * @return 添加后的信息
	// */
	// @RequestMapping(value = "/add", method = { RequestMethod.POST })
	// public @ResponseBody ServerResult addPost(ProWebAdvertisingCarrier
	// proWebAdvertisingCarrier) {
	// proWebAdvertisingCarrier.setAddTime(new Date());
	// proWebAdvertisingCarrierRepository.save(proWebAdvertisingCarrier);
	// return renderSuccess(proWebAdvertisingCarrier);
	// }

	/**
	 * 更新页面
	 * 
	 * @param 主键
	 * @return 更新目标页
	 */
	@RequestMapping(value = "/{primaryKey}/update", method = { RequestMethod.GET })
	public ModelAndView update(@PathVariable String primaryKey) {
		ProWebAdvertisingCarrier proWebAdvertisingCarrier = proWebAdvertisingCarrierRepository
				.get(ProWebAdvertisingCarrier.class, primaryKey);
		ModelAndView modelAndView = new ModelAndView("/proWebAdvertisingCarrier/update");
		modelAndView.addObject("info", proWebAdvertisingCarrier);
		return modelAndView;
	}

	/**
	 * 更新动作
	 * 
	 * @param 信息
	 * @return 更新后的信息
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public @ResponseBody ServerResult updatePost(ProWebAdvertisingCarrier proWebAdvertisingCarrier) {
		proWebAdvertisingCarrierRepository.update(proWebAdvertisingCarrier);
		return renderSuccess(proWebAdvertisingCarrier);
	}

	/**
	 * 删除
	 * 
	 * @param 主键
	 * @return 删除结果
	 */
	@RequestMapping(value = "/{primaryKey}/del", method = { RequestMethod.GET })
	public @ResponseBody Boolean del(@PathVariable String primaryKey) {
		proWebAdvertisingCarrierRepository.delete(ProWebAdvertisingCarrier.class, primaryKey);
		return true;
	}
}
